/*
 *    Copyright 2010 The maven-migrate-plugin Team
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package com.googlecode.mvnmigrate;

import java.io.File;
import java.io.PrintStream;
import java.text.MessageFormat;

import org.apache.ibatis.migration.commands.ScriptCommand;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;

/**
 * Goal which executes the ibatis migration script command.
 *
 * @version $Id: ScriptCommandMojo.java 154 2010-07-24 12:52:25Z marco.speranza79 $
 * @goal script
 */
public class ScriptCommandMojo extends AbstractCommandMojo<ScriptCommand> {

    /**
     * Initial version
     *
     * @parameter expression="${migration.v1}"
     */
    protected String v1;

    /**
     * Final version.
     *
     * @parameter expression="${migration.v2}"
     * @required
     */
    protected String v2;

    /**
     * The output file to be create.
     *
     * @parameter expression="${migration.output}"
     */
    protected File output;

    /**
     * Creates a instance of  script command mojo.
     */
    public ScriptCommandMojo() {
    }
    
    /**
     * {@inheritDoc}
     */
    @Override
    ScriptCommand createCommandClass() {
        return new ScriptCommand(repository, environment, force);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void execute() throws MojoExecutionException, MojoFailureException {
        if (isSkip()) return;

        try {
            init();
            
            if (this.output == null) {
                // Set the default System.out PrintStream
                this.getCommand().setPrintStream(System.out);
                if (this.getLog().isInfoEnabled()) {
                    String[] args = { this.v1, this.v2 };
                    MessageFormat format = new MessageFormat(getBundle(locale).getString("migration.plugin.execution.command.script.sqlscript"));
                    this.getLog().info(format.format(args));
                }
                System.out.println("  --- CUT HERE ---");
            } else {
                if (!this.output.exists()) {
                    new File(this.output.getParent()).mkdirs();
                }
                this.getCommand().setPrintStream(new PrintStream(this.output));
            }

            this.getCommand().execute(this.v1
                    + " "
                    + this.v2);

            if (this.getLog().isInfoEnabled()) {
                if (this.output != null) {
                    File[] args = { this.output };
                    MessageFormat format = new MessageFormat(getBundle(locale).getString("migration.plugin.execution.command.create.file"));
                    this.getLog().info(format.format(args));
                } else {
                    String[] args = { this.v1, this.v2 };
                    MessageFormat format = new MessageFormat(getBundle(locale).getString("migration.plugin.execution.command.script.sqlscript"));
                    this.getLog().info(format.format(args));
                }
            }
        } catch (RuntimeException e) {
            throw e;
        } catch (Exception e) {
            throw new MojoExecutionException(e.getMessage(), e);
        }
    }

}
